package com.itheima.travel.service.impl;

import com.itheima.travel.dao.UserDao;
import com.itheima.travel.domain.ResultInfo;
import com.itheima.travel.domain.User;
import com.itheima.travel.service.UserService;
import com.itheima.travel.utils.Md5Utils;
import com.itheima.travel.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
    public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public ResultInfo register(User user,String smsCode) {

        String authCode = (String) redisTemplate.opsForValue().get("smsCode" + user.getTelephone());

        if (authCode==null){
            return new ResultInfo(false,"验证码过期");
        }

        if (!authCode.equalsIgnoreCase(smsCode)){
            return new ResultInfo(false,"验证码错误");
        }

        User dbUser = userDao.findByUsername(user.getUsername());
        if (dbUser!=null){
            return new ResultInfo(false,"用户名已被注册！");
        }
        User dbTelephone = userDao.findByTelephone(user.getTelephone());
        if (dbTelephone!=null){
            return new ResultInfo(false,"手机号已被注册！");
        }
        String uuid = UuidUtils.simpleUuid();
        String md5 = Md5Utils.getMd5(user.getPassword() + uuid);
        user.setPassword(md5);
        user.setSalt(uuid);
        userDao.save(user);
        return new ResultInfo(true,"注册成功");
    }

    @Override
    public ResultInfo sendSms(String telephone) {
        //String code = SmsUtils.send(telephone, "黑马旅游网", "SMS_205126318", authCode);
        String authCode="123456";
        String code = "ok";
        if ("ok".equalsIgnoreCase(code)){
            redisTemplate.opsForValue().set("smsCode"+telephone,authCode,300, TimeUnit.SECONDS);
            return new ResultInfo(true,"发送成功");
        }else {
            return new ResultInfo(false,"发送失败");
        }
    }


    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    /**
     * 作用：登陆
     *
     * @param param 该map封装了用户名、密码、验证码三个数据
     * @return
     */
    @Override
    public ResultInfo login(Map<String, String> param) {
        String username = param.get("username");
        String password = param.get("password");
        String smsCode = param.get("smsCode");
//        1. 通过用户名查找用户
        User dbUser = userDao.findByUsername(username);
//        2. 如果没有找到，则返回用户名不存在
        if (dbUser==null){
            return new ResultInfo(false,"用户名不存在");
        }
//        3. 如果找到，判断密码是否正确
//        1. 拼接字符串=用户名+用户输入的密码+盐
//        2. 对密码使用md5加密
        String newMd5 = Md5Utils.getMd5(password + dbUser.getSalt());
//        4. 如果密码错误，则返回密码错误的结果
        if (!dbUser.getPassword().equalsIgnoreCase(newMd5)){
            return new ResultInfo(false,"用户名或者密码错误");
        }
//        5. 再从Redis中获取验证码，判断验证码是否为空
        String redisCode = (String) redisTemplate.opsForValue().get("smsCode" + dbUser.getTelephone());
//        6. 如果验证码为空，则表示验证码过期
        if (redisCode==null){
            return new ResultInfo(false,"验证码过期");
        }
//        7. 如果验证码不为空，则判断验证码是否正确
        if (!redisCode.equalsIgnoreCase(smsCode)){
            return new ResultInfo(false,"验证码错误");
        }
//        8. 如果验证码正确，则返回登录成功，并且封装user对象到data属性中
        return new ResultInfo(true,"登录成功",dbUser);
    }


    @Override
    public User getPersonal(Integer uid) {
        return userDao.getPersonal(uid);
    }

    @Override
    public void updatePersonal(String nickname, String sex, String birthday, String email, String username){
        userDao.updatePersonal(nickname,sex,birthday,email,username);
    }
}
